#pragma once
#include "ihtml_event_handler.h"

class CHtmlEditor;
class user_config;
class sys_info;
struct font_data;

class CHtmlInterface
{
	friend class CHtmlEditor;
public:

enum etable_op
{
	e_insert_table = 1, 
	e_insert_row, 
	e_insert_col, 
	e_del_row, 
	e_del_col
};
	
public:
	CHtmlInterface();
	~CHtmlInterface(void);

	BOOL op_table(int op_type);
	BOOL send_msg(UINT message, WPARAM wParam, LPARAM lParam, bool bSend = true);
	void update_ui(WPARAM wParam);
	void set_wnd(CHtmlEditor* p);
	void set_notify(ihtml_event_handler* p);
	BOOL is_load_finished(){return TRUE;}
	BOOL is_selected();
	CString get_select_text();
	BOOL set_default_html();
	BOOL init_cusor_pos();
	BOOL focus_window();
	BOOL scroll_to_end();
	BOOL set_doc_font(const user_config* info);
	BOOL set_doc_font(LPCTSTR name = DEFAULT_FONT_NAME, LPCTSTR size = DEFAULT_FONT_SIZE,
					  LPCTSTR cor = DEFAULT_FONT_COLOR, LPCTSTR biu = DEFAULT_FONT_STYLE);
	BOOL set_font(LPCTSTR name=0, LPCTSTR size=0, LPCTSTR cor=0, LPCTSTR biu=0);
	BOOL set_charset(LPCTSTR s = "gb2312");
	BOOL set_mode(BOOL b);
	void on_loading_finished();
	BOOL exec(UINT cmd, CComVariant v=0, BOOL bPromtUser=FALSE);
	void set_default_bg_color();
	void set_bg_color(LPCTSTR cor);
	BOOL cleanup_tags();
	BOOL clear();
	BOOL clear_and_init(LPCTSTR font_name, LPCTSTR font_size, LPCTSTR cor, LPCTSTR biu);
	BOOL clear_and_init(const user_config* info);
	BOOL set_bg_img(const CString& img);
	BOOL set_body_html(LPCTSTR xml);
	BOOL modify_style(const user_config* info, int x=-1, int y=-1);
	BOOL insert_xml(LPCTSTR msg, BOOL b_scroll_to_end = TRUE);
	BOOL insert_line(LPCTSTR cor, int size);
	BOOL insert_img(LPCTSTR fn, LPCTSTR id = NULL);
	BOOL insert_view_history_link();
	BOOL replace_img(LPCTSTR new_img, LPCTSTR old_img);
	CString get_body_html(int type);
	CString get_all_html();

public:
	static CString parse_to_html(LPCTSTR ptr_html_ex);
	static CString encode_html(LPCTSTR html);
	static CString gen_img_id();
	static CString gen_fn_with_time(LPCTSTR file_name, LPCTSTR type, LPCTSTR path);
	static CString color_to_str(COLORREF r);
	static COLORREF str_to_color(CString str_color);

	static CString build_html_text(LPCTSTR font_name, LPCTSTR font_size, 
		LPCTSTR cor, LPCTSTR biu, LPCTSTR text = NULL,
		LPCTSTR label_begin = "<DIV>", LPCTSTR label_end = "</DIV>");
	static CString build_html_text(const user_config* info, LPCTSTR text = NULL);
	static CString build_who_say(const user_config* info, LPCTSTR time = NULL, 
		const font_data* style = NULL,LPCTSTR szPreText=NULL);

	static void pre_process_ie_img_bug(CString& img);


	static CString decode_misc(LPCTSTR ptr_str)
	{

	}

	static CString misc_file_name_encode(LPCTSTR ptr_file_name)
	{
		CString str_file_name = ptr_file_name;
		str_file_name.Replace('\\', '/');
		str_file_name.Replace(" ", "%20");
		
		return str_file_name;
	}

	static CString misc_file_name_decode(LPCTSTR ptr_file_name)
	{
		CString str_file_name = ptr_file_name;
		str_file_name.Replace('/', '\\');
		str_file_name.Replace("%20", " ");

		return str_file_name;
	}

	static CString gen_html_file_path(LPCTSTR ptr_path)
	{
		CString str_path = ptr_path;
		str_path = _T("file:///") + str_path;
		str_path = misc_file_name_encode(str_path);

		return str_path;
	}

	static CString gen_normal_file_path(LPCTSTR ptr_html_path)
	{
		CString str_html_path = ptr_html_path;
		str_html_path.Replace(_T("file:///"), _T(""));
		str_html_path = misc_file_name_decode(str_html_path);
		
		return str_html_path;
	}


public:
	CHtmlEditor* ptr_wnd_;
	BOOL edit_mode_;
	BOOL finished_;
	ihtml_event_handler*	 call_back_;
};
